package com.guodu.dao.impl;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.PurchasingDistributionDao;
import com.guodu.entity.PurchasingDistribution;
import com.guodu.entity.PurchasingdisStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.vo.PurchasingDistributionQueryVo;
import com.guodu.vo.PurchasingDistributionVo;

@Repository("purchasingDistributionDao")
public class PurchasingDistributionDaoImpl  extends BaseDao implements PurchasingDistributionDao {

	@Override
	public void save(PurchasingDistribution purchasingDistribution) {
		// TODO Auto-generated method stub
		super.save(purchasingDistribution);
	}

	@Override
	public long findSizeByVo(PurchasingDistributionQueryVo pdq, Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from PurchasingDistribution m where 1=1");
		if(pdq!=null){
//			if(pdq.getFunitsId()!=null){
//				hql.append("    and exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+pdq.getFunitsId()+" )");
//			}
//			if(pdq.getUnitsId()!=null && !"".equals(pdq.getUnitsId())){
//				hql.append("    and unitsId="+pdq.getUnitsId()+")");
//			}
			if(pdq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+pdq.getFunitsId()+" ) or m.unitsId ="+pdq.getFunitsId()+" )");
			}
			if(pdq.getUnitsId()!=null && !"".equals(pdq.getUnitsId())){
				hql.append(" and  m.unitsId = "+pdq.getUnitsId()+"");
			}
			if (pdq.getDrawingnum()!=null && !"".equals(pdq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdq.getDrawingnum()+"%'");
			}
			if (pdq.getMaterialName()!=null && !"".equals(pdq.getMaterialName())) {
				hql.append("    and materialName like '%"+pdq.getMaterialName()+"%'");
			}
			if (pdq.getProcurementMethod()!=null && !"".equals(pdq.getProcurementMethod())) {
				hql.append("    and purchaseType like '%"+pdq.getProcurementMethod()+"%'");
			}
			if (pdq.getSpecifications()!=null && !"".equals(pdq.getSpecifications())) {
				hql.append("    and specifications like '%"+pdq.getSpecifications()+"%'");
			}
			if (pdq.getSupplier()!=null && !"".equals(pdq.getSupplier())) {
				hql.append("    and supplier like '%"+pdq.getSupplier()+"%'");
			}
			if (pdq.getTexture()!=null && !"".equals(pdq.getTexture())) {
				hql.append("    and texture like '%"+pdq.getTexture()+"%'");
			}
			if (pdq.getTbbeginDate()!=null && !"".equals(pdq.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+pdq.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdq.getTbendDate()!=null && !"".equals(pdq.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+pdq.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}	
			if(pdq.getStatus()!=null&&pdq.getStatus().trim().length()>0&&!pdq.getStatus().equals("5")){
				hql.append(" and status ="+pdq.getStatus());
			}
			if (pdq.getMonths()!=null && !"".equals(pdq.getMonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+pdq.getMonths()+"','yyyy-mm')");
			}
			if (pdq.getEmonths()!=null && !"".equals(pdq.getEmonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+pdq.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		return super.findInt(hql.toString());
	}
	@Override
	public void examinAll(PurchasingDistributionQueryVo pdq, Units units,
			UserInfo userInfo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		if(units==null){
			hql.append("update from PurchasingDistribution m set m.status=3 where m.status=0 ");
		}else if(units.getUnitsLevel()==1L){
			hql.append("update from PurchasingDistribution m set m.status=1 where m.status=0 ");
		}
		if(pdq!=null){
//			if(pdq.getFunitsId()!=null){
//				hql.append("    and exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+pdq.getFunitsId()+" )");
//			}
//			if(pdq.getUnitsId()!=null && !"".equals(pdq.getUnitsId())){
//				hql.append("    and unitsId="+pdq.getUnitsId()+")");
//			}
			if(pdq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+pdq.getFunitsId()+" ) or m.unitsId ="+pdq.getFunitsId()+" )");
			}
			if(pdq.getUnitsId()!=null && !"".equals(pdq.getUnitsId())){
				hql.append(" and  m.unitsId = "+pdq.getUnitsId()+"");
			}
			if (pdq.getDrawingnum()!=null && !"".equals(pdq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdq.getDrawingnum()+"%'");
			}
			if (pdq.getMaterialName()!=null && !"".equals(pdq.getMaterialName())) {
				hql.append("    and materialName like '%"+pdq.getMaterialName()+"%'");
			}
			if (pdq.getProcurementMethod()!=null && !"".equals(pdq.getProcurementMethod())) {
				hql.append("    and purchaseType like '%"+pdq.getProcurementMethod()+"%'");
			}
			if (pdq.getSpecifications()!=null && !"".equals(pdq.getSpecifications())) {
				hql.append("    and specifications like '%"+pdq.getSpecifications()+"%'");
			}
			if (pdq.getSupplier()!=null && !"".equals(pdq.getSupplier())) {
				hql.append("    and supplier like '%"+pdq.getSupplier()+"%'");
			}
			if (pdq.getTexture()!=null && !"".equals(pdq.getTexture())) {
				hql.append("    and texture like '%"+pdq.getTexture()+"%'");
			}
			if (pdq.getTbbeginDate()!=null && !"".equals(pdq.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+pdq.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdq.getTbendDate()!=null && !"".equals(pdq.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+pdq.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}	
			if(pdq.getStatus()!=null&&pdq.getStatus().trim().length()>0&&!pdq.getStatus().equals("5")){
				hql.append(" and status ="+pdq.getStatus());
			}
			if (pdq.getMonths()!=null && !"".equals(pdq.getMonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+pdq.getMonths()+"','yyyy-mm')");
			}
			if (pdq.getEmonths()!=null && !"".equals(pdq.getEmonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+pdq.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		super.executeUpdate(hql.toString());
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchasingDistribution> findByVo(PurchasingDistributionQueryVo pdq, Units units,UserInfo userInfo, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchasingDistribution m where 1=1");
		if(pdq!=null){
			if(pdq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+pdq.getFunitsId()+" ) or m.unitsId ="+pdq.getFunitsId()+" )");
			}
			if(pdq.getUnitsId()!=null && !"".equals(pdq.getUnitsId())){
				hql.append(" and  m.unitsId = "+pdq.getUnitsId()+"");
			}
			if (pdq.getDrawingnum()!=null && !"".equals(pdq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdq.getDrawingnum()+"%'");
			}
			if (pdq.getMaterialName()!=null && !"".equals(pdq.getMaterialName())) {
				hql.append("    and materialName like '%"+pdq.getMaterialName()+"%'");
			}
			if (pdq.getProcurementMethod()!=null && !"".equals(pdq.getProcurementMethod())) {
				hql.append("    and purchaseType like '%"+pdq.getProcurementMethod()+"%'");
			}
			if (pdq.getSpecifications()!=null && !"".equals(pdq.getSpecifications())) {
				hql.append("    and specifications like '%"+pdq.getSpecifications()+"%'");
			}
			if (pdq.getSupplier()!=null && !"".equals(pdq.getSupplier())) {
				hql.append("    and supplier like '%"+pdq.getSupplier()+"%'");
			}
			if (pdq.getTexture()!=null && !"".equals(pdq.getTexture())) {
				hql.append("    and texture like '%"+pdq.getTexture()+"%'");
			}
			if (pdq.getTbbeginDate()!=null && !"".equals(pdq.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+pdq.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdq.getTbendDate()!=null && !"".equals(pdq.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+pdq.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}		
			if(pdq.getStatus()!=null&&pdq.getStatus().trim().length()>0&&!pdq.getStatus().equals("5")){
				hql.append(" and status ="+pdq.getStatus());
			}
			if (pdq.getMonths()!=null && !"".equals(pdq.getMonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+pdq.getMonths()+"','yyyy-mm')");
			}
			if (pdq.getEmonths()!=null && !"".equals(pdq.getEmonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+pdq.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		hql.append(" order by purchaseId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public PurchasingDistribution findById(String purchaseId) {
		// TODO Auto-generated method stub
		return (PurchasingDistribution) super.findById(PurchasingDistribution.class, purchaseId);
	}

	@Override
	public void update(PurchasingDistribution purchasingDistribution) {
		// TODO Auto-generated method stub
		super.update(purchasingDistribution);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchasingdisStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return super.findByProperty(PurchasingdisStatus.class, "purchaseId", purchaseId);
	}

	@Override
	public void changeStatus(List<PurchasingdisStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update PurchasingDistribution set status =");
		hql.append(status);
		hql.append(" where purchaseId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		super.deleteAllById(PurchasingDistribution.class, delIds);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchasingDistribution> findAllByVo(
			PurchasingDistributionQueryVo pdq, Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchasingDistribution m where 1=1");
		if(pdq!=null){
			if(pdq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+pdq.getFunitsId()+" ) or m.unitsId ="+pdq.getFunitsId()+" )");
			}
			if(pdq.getUnitsId()!=null && !"".equals(pdq.getUnitsId())){
				hql.append(" and  m.unitsId = "+pdq.getUnitsId()+"");
			}
			if (pdq.getDrawingnum()!=null && !"".equals(pdq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdq.getDrawingnum()+"%'");
			}
			if (pdq.getMaterialName()!=null && !"".equals(pdq.getMaterialName())) {
				hql.append("    and materialName like '%"+pdq.getMaterialName()+"%'");
			}
			if (pdq.getProcurementMethod()!=null && !"".equals(pdq.getProcurementMethod())) {
				hql.append("    and purchaseType like '%"+pdq.getProcurementMethod()+"%'");
			}
			if (pdq.getSpecifications()!=null && !"".equals(pdq.getSpecifications())) {
				hql.append("    and specifications like '%"+pdq.getSpecifications()+"%'");
			}
			if (pdq.getSupplier()!=null && !"".equals(pdq.getSupplier())) {
				hql.append("    and supplier like '%"+pdq.getSupplier()+"%'");
			}
			if (pdq.getTexture()!=null && !"".equals(pdq.getTexture())) {
				hql.append("    and texture like '%"+pdq.getTexture()+"%'");
			}
			if (pdq.getTbbeginDate()!=null && !"".equals(pdq.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+pdq.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdq.getTbendDate()!=null && !"".equals(pdq.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+pdq.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}		
			if(pdq.getStatus()!=null&&pdq.getStatus().trim().length()>0&&!pdq.getStatus().equals("5")){
				hql.append(" and status ="+pdq.getStatus());
			}
			if (pdq.getMonths()!=null && !"".equals(pdq.getMonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+pdq.getMonths()+"','yyyy-mm')");
			}
			if (pdq.getEmonths()!=null && !"".equals(pdq.getEmonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+pdq.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		hql.append("order by createTime desc");
		return super.find(hql.toString());
	}

	@Override
	public PurchasingDistributionVo findAllMoney(
			PurchasingDistributionQueryVo pdq, Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select sum(purchaseAmount),sum(marketPrice),sum(purchaseSave) from PurchasingDistribution m where 1=1");
		if(pdq!=null){
			if(pdq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+pdq.getFunitsId()+" ) or m.unitsId ="+pdq.getFunitsId()+" )");
			}
			if(pdq.getUnitsId()!=null && !"".equals(pdq.getUnitsId())){
				hql.append(" and  m.unitsId = "+pdq.getUnitsId()+"");
			}
			if (pdq.getDrawingnum()!=null && !"".equals(pdq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdq.getDrawingnum()+"%'");
			}
			if (pdq.getMaterialName()!=null && !"".equals(pdq.getMaterialName())) {
				hql.append("    and materialName like '%"+pdq.getMaterialName()+"%'");
			}
			if (pdq.getProcurementMethod()!=null && !"".equals(pdq.getProcurementMethod())) {
				hql.append("    and purchaseType like '%"+pdq.getProcurementMethod()+"%'");
			}
			if (pdq.getSpecifications()!=null && !"".equals(pdq.getSpecifications())) {
				hql.append("    and specifications like '%"+pdq.getSpecifications()+"%'");
			}
			if (pdq.getSupplier()!=null && !"".equals(pdq.getSupplier())) {
				hql.append("    and supplier like '%"+pdq.getSupplier()+"%'");
			}
			if (pdq.getTexture()!=null && !"".equals(pdq.getTexture())) {
				hql.append("    and texture like '%"+pdq.getTexture()+"%'");
			}
			if (pdq.getTbbeginDate()!=null && !"".equals(pdq.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+pdq.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdq.getTbendDate()!=null && !"".equals(pdq.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+pdq.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}		
			if(pdq.getStatus()!=null&&pdq.getStatus().trim().length()>0&&!pdq.getStatus().equals("5")){
				hql.append(" and status ="+pdq.getStatus());
			}
			if (pdq.getMonths()!=null && !"".equals(pdq.getMonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+pdq.getMonths()+"','yyyy-mm')");
			}
			if (pdq.getEmonths()!=null && !"".equals(pdq.getEmonths())) {
				hql.append("    and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+pdq.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		PurchasingDistributionVo vo = new PurchasingDistributionVo();
		List<Object[]> objs = super.find(hql.toString());
		if(objs.size()>0){
			DecimalFormat df = new DecimalFormat("##,###.##");
			try {
				if(objs.get(0)[0]!=null){
					vo.setAllMoney1(df.format(df.parse(objs.get(0)[0].toString())));
				}
				if(objs.get(0)[1]!=null){
					vo.setAllMoney2(df.format(df.parse(objs.get(0)[1].toString())));
				}
				if(objs.get(0)[2]!=null){
					vo.setAllMoney3(df.format(df.parse(objs.get(0)[2].toString())));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		return vo;
	}

	

}
